#include <iostream>

using namespace std;

// 328. 奇偶链表
// https://leetcode.cn/problems/odd-even-linked-list/description/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution
{
public:
    ListNode *oddEvenList(ListNode *head)
    {
        ListNode *curPtr = head;
        ListNode *oddPtr = nullptr;
        ListNode *evenPtr = nullptr;
        ListNode *pre = nullptr;
        for (int i = 0; curPtr != nullptr; i++)
        {
            ListNode *nextPtr = curPtr->next;
            if (i % 2 == 0)
            {
                if (evenPtr == nullptr)
                {
                    evenPtr = curPtr;
                }
                else
                {
                    evenPtr->next = curPtr;
                    evenPtr = curPtr;
                }
            }
            else
            {
                if (oddPtr == nullptr)
                {
                    oddPtr = curPtr;
                    pre = oddPtr;
                }
                else
                {
                    oddPtr->next = curPtr;
                    oddPtr = curPtr;
                }
            }
            curPtr->next = nullptr;
            curPtr = nextPtr;
        }
        if (evenPtr != nullptr)
        {
            evenPtr->next = pre;
        }

        return head;
    }
};

int main()
{
    return 0;
}